package com.maibow.entity.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 解析网络请求数据是否成功对象， datas 数据为结果字符串的有效数据，需另外解析
 * JsonObject.parseObject success
 * 是否成功标志
 * 
 * @author Administrator
 *
 */
public class NetResultDataEntityV2 {
    public static final <T> T parseResultObject(NetResultDataEntityV2 resultDataEntity, Class<T> classType) {
        DataEntity dataResult = getDatas(resultDataEntity);
        T resultObj = null;
        try {
            resultObj = JSON.parseObject(dataResult.toString(), classType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultObj;
    }

    public static NetResultDataEntityV2 parseDataResultEntity(String result) {
        try {
            NetResultDataEntityV2 resultEntity = JSONObject.parseObject(result, NetResultDataEntityV2.class);
            return resultEntity;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 判断是否成功
     * 
     * @param resultDataEntity
     * @return
     */
    public static boolean isSuccess(NetResultDataEntityV2 resultDataEntity) {
        if (resultDataEntity != null) {
            return resultDataEntity.isSuccess();
        } else {
            return false;
        }
    }

    /**
     * 获取失败信息
     * 
     * @param resultDataEntity
     * @return
     */
    public static String getErrorMsg(NetResultDataEntityV2 resultDataEntity) {
        if (resultDataEntity != null) {
            return resultDataEntity.getErrorMsg() == null ? "" : resultDataEntity.getErrorMsg();
        } else {
            return "";
        }
    }

    /**
     * 获取数据，有可能为空
     * 
     * @param resultDataEntity
     * @return
     */
    public static DataEntity getDatas(NetResultDataEntityV2 resultDataEntity) {
        if (resultDataEntity != null) {
            return resultDataEntity.getDatas();
        }
        return null;
    }

    private String errorMsg;

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    private DataEntity datas;

    private boolean success;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * datas
     *
     * @return  the datas
     * @since   1.0.0
     */
    
    public DataEntity getDatas() {
        return datas;
    }

    /**
     * @param datas the datas to set
     */
    public void setDatas(DataEntity datas) {
        this.datas = datas;
    }

}
